package com.enjoy.concurrent.ch8;

import java.util.Random;

/**
 * 业务代码提交任务
 */
public class MyTask implements ITaskProcessor<Integer, Integer> {

    @Override
    public TaskResult<Integer> taskExecute(Integer data) {
        Random random = new Random();
        int i = random.nextInt(500);
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if ((i <= 300)) {

            return new TaskResult<Integer>(TaskResultType.SUCCESS, i);
        } else if (i <= 400) {
            return new TaskResult<>(TaskResultType.FAIL, -1, "处理失败");
        } else {
            try {
                throw new RuntimeException("发生异常");
            } catch (Exception e){
                return new TaskResult<>(TaskResultType.EXCEPTION, -1, e.getMessage());
            }
        }
    }
}
